package com.net.ip;

import java.io.IOException;
import java.net.InetAddress;
import jpcap.*;
import jpcap.packet.*;

@SuppressWarnings("unused")
public class PacketsCapter {

	private NetworkInterface[] devices;
	private Integer cap_index;
	public JpcapCaptor captor;

	public void IpPackets(int index) throws IOException {
		NetworkInterface[] devices = JpcapCaptor.getDeviceList();

		JpcapCaptor captor = JpcapCaptor.openDevice(devices[index], 65535,
				false, 20);
		captor.setFilter("ip and tcp", true);
		IPPacket ip_packet;
		Packet pcaket;
		while (true) {
			ip_packet = (IPPacket) captor.getPacket();
			if (ip_packet != null) {
				System.out.println("ip");
				System.out.println("src_ip :" + ip_packet.src_ip);
				System.out.println("dst_ip :" + ip_packet.dst_ip);
				System.out.println("length :" + ip_packet.length);
			}
		}
	}

	public void TcpPackets(int index) throws IOException {
		NetworkInterface[] devices = JpcapCaptor.getDeviceList();

		JpcapCaptor captor = JpcapCaptor.openDevice(devices[index], 65535,
				false, 20);
		captor.setFilter("ip and tcp", true);
		TCPPacket tcp_packet;
		Packet pcaket;
		while (true) {
			tcp_packet = (TCPPacket) captor.getPacket();
			if (tcp_packet != null) {
				System.out.println("shit_tcp");
			}
		}
	}

	public void UdpPackets(int index) throws IOException {
		NetworkInterface[] devices = JpcapCaptor.getDeviceList();

		JpcapCaptor captor = JpcapCaptor.openDevice(devices[index], 65535,
				false, 20);
		captor.setFilter("ip and udp", true);
		UDPPacket udp_packet;
		Packet pcaket;
		while (true) {
			udp_packet = (UDPPacket) captor.getPacket();
			if (udp_packet != null) {
				System.out.println("shit-- udp");
			}
		}
	}

	public void openDevies() throws IOException {
		this.captor = JpcapCaptor.openDevice(devices[cap_index], 65535, false,
				20);
	}

	public void getDevics() {
		this.devices = JpcapCaptor.getDeviceList();
	}

	public void setFilter() throws IOException {
		this.captor.setFilter("ip and tcp", true);
	}

	public Packet getPacket() {
		return this.captor.getPacket();
	}

	public void setCapIndex(Integer index) {
		this.cap_index = index;
	}

}
